package com.vx.ratingTool.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vx.ratingTool.entities.Player;
import com.vx.ratingTool.entities.SelectedPlayer;
import com.vx.ratingTool.services.IPlayerBaseManager;

@Controller
public class PlayerActions {
	@Autowired
	private IPlayerBaseManager playerBaseManager;
	@Autowired
	private SelectedPlayer selected_player;

	@RequestMapping(value = { "/createplayers" }, method = RequestMethod.GET)
	public String createPlayersRequest(Model model) {

		playerBaseManager.clearDatabase();
		playerBaseManager.saveList(playerBaseManager.generatePlayers(1000));

		model.addAttribute("players", playerBaseManager.getListOfPlayers());

		return "createplayers";
	}

	@RequestMapping(value = { "/createCustomPlayer" }, method = RequestMethod.GET)
	public void setupForm() {

	}

	@RequestMapping(value = { "/createCustomPlayer" }, method = RequestMethod.POST)
	public String createCustomPlayer(@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("skill") int skill,
			@RequestParam("rating") int rating, @RequestParam("placements") int placements, Model model) {

		playerBaseManager.addPlayer(new Player(id, name, skill, rating, placements, 0));

		model.addAttribute("success", "Player #" + id + " <" + playerBaseManager.getById(id).getName() + "> created!");

		return "createCustomPlayer";
	}

	@RequestMapping(value = "/selectPlayer", method = RequestMethod.GET)
	public void getSelectedPlayerRequest(Model model) {
		if (selected_player.getId() != -1)
			model.addAttribute("selected", "Selected player: " + playerBaseManager.getById(selected_player.getId()).getName());
		else {
			model.addAttribute("selected", "Selected player: none");
		}
	}

	@RequestMapping(value = "/selectPlayer", method = RequestMethod.POST)
	public String SetSelectedPlayerRequest(@RequestParam("id") int id, Model model) {
		selected_player.setId(id);
		model.addAttribute("selected", "Selected player: " + playerBaseManager.getById(selected_player.getId()).getName());

		return "selectPlayer";
	}

	@RequestMapping(value = "/showPlayers", method = RequestMethod.GET)
	public String showPlayersRequest(Model model) {

		model.addAttribute("players", playerBaseManager.getListOfPlayers());

		return "showPlayers";
	}
}
